<template>
    <div class="app-container">
        <div v-show="type == 0 || type == 2">
            <el-row>
                <el-col :span="24">
                    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                        label-width="68px">
                        <el-form-item label="项目名称" prop="projectName">
                            <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="计划时间" prop="startTime">
                            <el-date-picker clearable v-model="queryParams.startTime" type="date"
                                value-format="yyyy-MM-dd" placeholder="请选择计划开始时间" style="width: 170px;">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="-" prop="endTime" label-width="16px">
                            <el-date-picker clearable v-model="queryParams.endTime" type="date"
                                value-format="yyyy-MM-dd" placeholder="请选择计划结束时间" style="width: 170px;">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini"
                                @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                    <el-row :gutter="10" class="mb8">
                        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                    </el-row>

                    <el-table v-loading="loading" :data="projectInfoList" :tree-props="{ children: 'children' }"
                        row-key="id" efault-expand-all @selection-change="handleSelectionChange" highlight-current-row
                        size="mini" height="450" :row-style="{ height: '30px' }" border
                        :cell-style="{ padding: '0px' }">
                        <el-table-column label="序号" type="index" align="center" prop="index"
                            :index="indexMethod"></el-table-column>
                        <el-table-column label="项目编号" align="center" prop="projectNumber" width="120"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true"
                            width="220">
                            <template slot-scope="scope">
                                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                                <el-button size="small" @click="handleView(scope.row, scope.$index + 1)" type="text">
                                    {{ scope.row.projectName }}
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="项目负责人" align="center" prop="projectLeader" width="90"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="计划开始时间" align="center" prop="startTime" width="120"
                            :show-overflow-tooltip="true">

                            <template slot-scope="scope">
                                <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
                            </template></el-table-column>
                        <el-table-column label="计划结束时间" align="center" prop="endTime" width="120"
                            :show-overflow-tooltip="true">

                            <template slot-scope="scope">
                                <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                            </template></el-table-column>
                        <el-table-column label="预算总额" align="center" prop="budgetAmount" width="80"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="成本总额" align="center" prop="totalCost" width="80"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="客户满意度" align="center" prop="customerSatisfaction" width="90"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="状态" align="center" prop="state">

                            <template slot-scope="scope">
                                <dict-tag :options="dict.type.project_status" :value="scope.row.state" />
                            </template>
                        </el-table-column>
                        <el-table-column label="创建人" align="center" prop="creationName" :show-overflow-tooltip="true" />
                        <el-table-column label="创建时间" align="center" prop="creationTime" width="120"
                            :show-overflow-tooltip="true">

                            <template slot-scope="scope">
                                <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">

                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-user"
                                    @click="handleUpdate(scope.row, scope.$index + 1)" title="编辑"
                                    v-if="(scope.row.sign == '主') && (scope.row.contractReviewer === null ? true : (scope.row.contractReviewer==userName)) && (scope.row.state=='1')">
                                </el-button>
                                <el-button size="mini" type="text" icon="el-icon-view"
                                    @click="handleView(scope.row, scope.$index + 1)" title="查看"> </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize" @pagination="getList" />
                </el-col>
            </el-row>
        </div>
        <!-- 添加或修改【请填写功能名称】对话框 -->
        <el-dialog title="查看子项目信息" :visible.sync="openSub" width="900px" append-to-body>
            <el-form ref="formSub" :model="formSub" label-width="100px">
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="项目编号" prop="projectNumber" label-width="110px">
                            <el-input v-model="formSub.projectNumber" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">

                        <el-form-item label="项目名称" prop="projectName" label-width="110px">
                            <el-input v-model="formSub.projectName" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="费用" prop="cost" label-width="110px">
                            <el-input v-model="formSub.cost" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="执行人" prop="executorName" label-width="110px">
                            <el-input v-model="formSub.executorName" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="外协公司" prop="outsourcingCompany" label-width="110px">
                            <el-input v-model="formSub.outsourcingCompany" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="类型" prop="type" label-width="110px">
                            <el-select v-model="formSub.type" style="width: 284px;" disabled="disabled">
                                <el-option v-for="dict in dict.type.sub_project_type" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="产品名称" prop="productName" label-width="110px">
                            <el-input v-model="formSub.productName" placeholder="点击选择产品" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="创建人" prop="creationName" label-width="110px">
                            <el-input v-model="formSub.creationName" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="计划开始时间" prop="startTime" label-width="110px">
                            <el-input v-model="formSub.startTime" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="计划结束时间" prop="endTime" label-width="110px">
                            <el-input v-model="formSub.endTime" disabled="disabled" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="阶段" prop="subProjectStage" label-width="110px">
                            <el-input v-model="formSub.subProjectStage" disabled="disabled" style="width:678px" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelSub">关 闭</el-button>
            </div>
        </el-dialog>
        <div v-if="type == 1">
            <Operate @change="close" :opt="edit" :id="id" :index="index"></Operate>
        </div>
    </div>
</template>

<script>
import { listSubProjectInfo, listSubProjectTree } from "@/api/project/projectInfo";
import { listSubInfo, getSubInfo, delSubInfo, addSubInfo, updateSubInfo } from "@/api/project/subInfo";
import { listStage, getStage, delStage, addStage, updateStage } from "@/api/project/stage";
import MainType from "@/components/MainType/index";
import { getToken } from "../../../utils/auth";
import Operate from "./operate.vue";
import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
    components: { MainType, Operate, Treeselect },
    dicts: ['project_status', 'sub_project_type'],
    name: "ProjectInfo",
    data() {
        return {
            isTree: false,
            openSub: false,
            typeCode: 'project',
            type: 0,
            opt: 'add',
            id: '',
            obj: null,
            index: null,
            edit: 'edit',
            stageProjectList: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            userName: "",
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 项目信息表格数据
            projectInfoList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 15,
                projectName: null,
                state: null,
                covenantId: null,
                startTime: null,
                endTime: null,
                attackTime: null,
                completeTime: null,
            },
            querySubParams: {
                type: null,
            },
            // 表单参数
            form: {},
            formSub: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.userName = this.$store.state.user.info.nickName;
        console.log(this.userName)
        this.getList();
    },
    methods: {
        indexMethod(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        //树点击事件
        getNode(data, node, obj) {
            this.queryParams.classificationId = data.id
            this.getList()
        },
        /** 查询项目信息列表 */
        getList() {
            this.loading = true;
            this.queryParams.state = 1;
            this.queryParams.covenantId = this.$store.state.user.info.userId;
            listSubProjectTree(this.queryParams, this.querySubParams).then(response => {
                this.projectInfoList = response.rows;
                console.log(this.projectInfoList)
                this.total = response.total;
                this.loading = false;
            });
        },
        close(data) {
            this.type = 0
            this.getList();
            this.open = data;

        },
        cancelSub() {
            this.openSub = false;
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        resetSub() {
            this.formSub = {
                id: null,
                projectId: null,
                projectNumber: null,
                projectName: null,
                productId: null,
                productName: null,
                subProjectStage: null,
                outsourcingCompany: null,
                executorId: null,
                executorName: null,
                cost: null,
                type: null,
                startTime: null,
                endTime: null,
                creationId: null,
                creationName: null,
                creationTime: null
            };
            this.resetForm("formSub");
        },
        // 表单重置
        reset() {
            this.form = {
                projectId: null,
                classificationId: null,
                projectNumber: null,
                projectName: null,
                contractId: null,
                contractName: null,
                projectLeaderId: null,
                projectLeader: null,
                executorId: null,
                executorName: null,
                startTime: null,
                endTime: null,
                attackTime: null,
                completeTime: null,
                state: null,
                operateTime: null,
                operateId: null,
                operateName: null,
                departmentId: null,
                departmentName: null,
                organizationId: null,
                organizationName: null,
                creationId: null,
                creationName: null,
                creationTime: null,
                belongTo: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.projectId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
            this.edit = "edit"
            this.id = row.id;
            this.type = 1
        },
        handleView(row, index) {
            if (row.sign == "主") {
                this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
                this.edit = "mainView"
                this.id = row.id;
                this.type = 1
            } else if (row.sign == "子") {
                this.resetSub();
                this.openSub = true;
                var id = row.id;
                getSubInfo(id).then(response => {
                    this.formSub = response.data;
                  listStage(params).then(response => {
                    this.stageProjectList = response.rows;
                    var list = this.stageProjectList;
                    console.log(list[0].stageName)
                    this.formSub.subProjectStage = list.length > 0 ? list[0].stageName : "";
                  });
                });
                const params = {
                    projectId: id
                }
            }
        }
    }
};
</script>
